import router from '@/router/index'

/**
 * 增强方法
 */

// 增强push 方法
const cachePush = router.push
// @ts-ignore
router.push = function(history: any, successCb: any, errorCb: any) {
  if (successCb || errorCb) {
    // @ts-ignore
    return cachePush.call(this, history, successCb, errorCb)
  } else {
    // @ts-ignore
    return cachePush.call(this, history, () => {
    })
  }
}

const cacheReplace = router.replace
// @ts-ignore
router.replace = function(history: any, successCb: any, errorCb: any) {
  if (successCb || errorCb) {
    // @ts-ignore
    return cacheReplace.call(this, history, successCb, errorCb)
  } else {
    // @ts-ignore
    return cacheReplace.call(this, history, () => {
    })
  }
}
// 增强push replace 方法
// const methods = ['push', 'replace']
// methods.forEach(item => {
//   const cacheMethods = router[item]
//   router[item] = function(history, successCb, errorCb) {
//     // console.log(this) VueRouter的实例
//     if (successCb || errorCb) {
//       return cacheMethods.call(this, history, successCb, errorCb)
//     } else {
//       return cacheMethods.call(this, history).catch(err => err)
//     }
//   }
// })
